package com.gin.kotlin.no_gay_alliance.database.entity

import android.os.Parcelable
import androidx.room.Entity
import androidx.room.PrimaryKey
import com.gin.nga.cookie.NgaCookie
import kotlinx.parcelize.Parcelize

/**
 * NGA账号
 * @property userId 用户id
 * @property username 用户名
 * @property cid cid
 * @property enable 是否为当前账号
 * @property autoCheckIn 自动签到开关
 * @property autoMission 自动完成任务开关
 * @constructor
 */
@Entity
@Parcelize
class NgaAccount(
    @PrimaryKey
    var userId: Long,
    var username: String,
    var cid: String,
    var enable:Boolean = false,
    var autoCheckIn:Boolean = false,
    var autoMission:Boolean = false,
) : Parcelable {

    constructor(cookie: NgaCookie) : this(
        userId = cookie.userId,
        username = cookie.username,
        cid = cookie.cid,
    )

    constructor() : this(0,"","")

    fun toCookie() = NgaCookie(userId, username, cid)

    /**
     * 导出字符串
     * @return String
     */
    fun toExportString() = listOf(userId, username, cid).joinToString(SEPARATOR) { it.toString() }

    /**
     * 从导出字符串中解析
     * @param s String
     * @return List<String>
     */
    fun fromExportString(s: String) = also { a ->
        s.split(SEPARATOR).also {
            a.userId = it[0].toLong()
            a.username = it[1]
            a.cid = it[2]
        }
    }

    companion object {
        const val SEPARATOR = "|-|"
    }
}